<div class="row animate fadeIn">
  <?php if($operation == 'edit') { ?>
  <!-- Edit Request Form -->
    <form class="form-horizontal" id="ed_req_data">
    <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Edit</i></h5>
        <div class="ibox-tools">    
             <a class="btn btn-primary btn-sm save-btn-tab" id="edit_req_btn">Save</a>
             <a id="cancel_add" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
        </div>
     </div>
     <div class="ibox-content">
        <?php foreach ($datas as $data) { ?>
            <input type="hidden" name="h_reqedit_id" value="<?php echo $req_id; ?>">
            <div class="form-group">
                <label class="col-lg-3 control-label">Request Number:</label>
                <div class="col-lg-5">
                    <input type="text" id=""class="form-control" name="ed_reqNum" value="<?php echo $data->inv_request_num; ?>"  readonly>
                </div>
             </div>
             <div class="form-group">
                <label class="col-lg-3 control-label">Request Date:</label>
                 <div class="col-lg-5">
                    <input type="date" id=""class="form-control" name="ed_reqDate" value="<?php echo $data->inv_request_date; ?>"  >
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Requested By:</label>
                 <div class="col-lg-5">
                    <select class = "chosen-select" name="ed_reqBy" id ="">
                      <option value=""> -- </option>
                      <?php foreach ($users as $usr) { ?>
                        <option value="<?php echo $usr->user_id; ?>" id="" <?php echo ($data->inv_requested_by == $usr->user_id ? 'selected' : '' ); ?> ><?php echo $usr->user_name." (".$usr->last_name.", ".$usr->first_name.")"; ?></option>
                      <?php } ?>
                    </select>
                    
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Approved By:</label>
                <div class="col-lg-5">
                    <input type="text" id=""class="form-control" name="" value=""  readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Status:</label>
                <div class="col-lg-5">
                    <select class = "chosen-select" name="ed_reqStat" id ="ed_rs">
                      <!-- <option value=""> -- </option> -->
                      <?php foreach ($status as $stat) { ?>
                        <option value="<?php echo $stat->lookup_code; ?>" id="" <?php echo ($stat->lookup_code == 'REQ_SUBMITTED' ? 'selected' : ''); ?> ><?php echo $stat->meaning; ?></option>
                      <?php } ?>
                    </select>
                </div>
            </div>
      </div>
      <?php } ?>  
    </form>
  <!-- END of Edit Request Form -->  
  <?php } ?>

  <?php if($operation == 'apprv') { ?>
  <!-- Approval Form -->
    <form class="form-horizontal" id="trans_inv_form">
    <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Approve Request</i></h5>
        <div class="ibox-tools">    
             <a class="btn btn-primary btn-sm save-btn-tab" id="approve_req">Save</a>
             <a id="cancel_apprv" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
        </div>
     </div>
     <div class="ibox-content">
        <?php foreach ($datas as $data) { ?>
            <div class="form-group">
                <label class="col-lg-3 control-label">Request Number:</label>
                <div class="col-lg-5">
                    <input type="text" id=""class="form-control" name="" value="<?php echo $data->inv_request_num; ?>"  readonly>
                </div>
             </div>
             <div class="form-group">
                <label class="col-lg-3 control-label">Request Date:</label>
                 <div class="col-lg-5">
                    <input type="date" id="" class="form-control" name="" value="<?php echo $data->inv_request_date; ?>"  readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Requested By:</label>
                 <div class="col-lg-5">
                    <input type="text" id="" class="form-control" name="" value="<?php echo $data->user_name.' ('.$data->last_name.', '.$data->first_name.')'; ?>"  readonly>
                </div>
            </div>
            <!-- <div class="form-group">
                <label class="col-lg-3 control-label">Approved By:</label>
                <div class="col-lg-5">
                    <input type="text" id=""class="form-control" name="" value="<?php ?>"  readonly>
                </div>
            </div> -->
            <div class="form-group">
                <label class="col-lg-3 control-label">Status:</label>
                <div class="col-lg-5">
                   <select class = "chosen-select" name="apprv_stat" id ="aprv_s" >
                      <?php foreach ($status as $stat) {  
                        if(!($stat->lookup_code == 'REQ_SUBMITTED' || $stat->lookup_code == 'REQ_RELEASED' || $stat->lookup_code == 'REQ_PRE-RELEASED'  )) {?>
                        <option value="<?php echo $stat->lookup_code; ?>" id="" <?php echo ($stat->lookup_code == $data->inv_request_status ? 'selected' : ''); ?> ><?php echo $stat->meaning; ?></option>
                      <?php } } ?>
                    </select>
                </div>
            </div>
        <?php } ?>    
      </div>
    </form>
  <?php } ?>

    <div id="list_sequence">
        <div id="" class="ibox-content">
          <h3 style="color: #00afef">Requested Item</h3>
          <?php if($operation == 'edit') { ?>  
            <form  id="ed_add_content" class="form-inline"> 
          <?php } ?>
                <input type="hidden" id="" name="c_reqId_ed" value="<?php echo $req_id; ?>">
                    <table id="pgTable"class="table table-bordered" style="width:80%;">
                      <thead>
                        <tr>
                          <th style="width:100px;">Sequence</th>
                          <th>Item</th>
                          <th style="width:100px;">Qty</th>
                          <th style="width:50px;">Action</th>
                        </tr>
                      <?php if($operation == 'edit') { ?>  
                        <tr>
                          <td style="margin:0px;padding:0px;">
                              <input style="width:100%;text-align: right;" type="text" id="" name="ed_seq" class="form-control-custom">
                          </td>
                          <td style="margin:0px;padding:0px;">
                              <select id="item_fk" class="chosen-select" name="ed_item">
                                  <option value=""> -- </option>
                                  <?php foreach ($items as $item) { ?>
                                      <option value="<?php echo $item->item_id.'  ('.$item->description.')'; ?>"><?php echo $item->item_num.'  ('.$item->description.')'; ?></option>
                                  <?php } ?>
                              </select>
                          </td>
                          <td style="margin:0px;padding:0px;">
                            <input style="width:100%;text-align: right;" type="text" id="" name="ed_qty" class="form-control-custom">
                          </td>
                          <td style="margin:0px;padding:0px;">
                              <a style="width:100%;" class="btn btn-white" id="add_con_ed"><i class="fa fa-plus success"></i></a>
                          </td>
                          
                        </tr>
                      <?php } ?>  
                      </thead>
                    <?php if($operation == 'edit') { ?>    
                      </form>
                    <?php } ?>  
                      <tbody id="tbody_editCon" class="animate fadeIn">
                                                        
                      </tbody>
                    </table> 
        </div>
      </div>
 
    
 </div>
 <script>
    $(document).ready(function(){
   
      $('#ed_req_data').data('serialize',$('#ed_req_data').serialize());
    
    $('#add_locator_sched').hide();
    $(".chosen-select").chosen({width:"100%;"}); 
    var req_id    = <?php echo json_encode($req_id);    ?>;
    var operation = <?php echo json_encode($operation); ?>;
    generate_reqContents(req_id);
    $('#ed_rs option:not(:selected)').attr('disabled', true).trigger('chosen:updated');

    $('#gana').click(function(){
        $('#tbodyadd').append("<tr><td>"+$('#sample_seq').val()+"</td><td>"+$('#sample_item').val()+"</td><td>"+$('#sample_qty').val()+"</td><td></td></tr>");
        $('#sample_seq').val(''); $('#sample_item').val('1').change();  $('#sample_qty').val('');
    });   

    function generate_reqContents(id){
      $.ajax({
            type: "POST",
            url: "<?php echo base_url($this->session->userdata('forajax').'/view_req_contents'); ?>"+"/"+id,
            dataType: 'json',
        success: function(data){
              $('#tbody_editCon').empty();
              var trHTML = '';
              if(operation == 'apprv'){
                $.each(data, function(i, item) {
                  trHTML += '<tr><input id="reqcon_id" type="hidden" value="'+ data[i].req_content_id +'"><td>' + data[i].sequence_num + '</td><td text-align:right;>' + data[i].item_num + ' ('+data[i].description+')' + '</td><td text-align:right;>' + data[i].inv_item_qty + '</td><td></td></tr>';               
                });
              }else if(operation == 'edit'){
                $.each(data, function(i, item) {
                  trHTML += '<tr><input id="reqcon_id" type="hidden" value="'+ data[i].req_content_id +'"><td>' + data[i].sequence_num + '</td><td text-align:right;>' + data[i].item_num + ' ('+data[i].description+')' + '</td><td text-align:right;>' + data[i].inv_item_qty + '</td><td style="margin:0px;padding:0px;"><a style="width:100%;" class="btn btn-outline btn-danger del" id="ok_x"><i id="del_icon" class="fa fa-times success"></i></a></td></tr>';               
                });
              }
                  $('#tbody_editCon').append(trHTML);
                }      
            });
    }

    $(document).on('click','.btn.btn-outline.btn-danger.del',function(){
              var sad = $(this);
              var line_id    = $(this).closest('tr').children('#reqcon_id').val();
              swal({
                title: "Are you sure?",
                text: "This line in sequence will be deleted !",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Yes, delete it!",
                closeOnConfirm: false
            }, function (isConfirm) {
              if (isConfirm) {
                $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/delete_req_contents'); ?>",
                        data: {"line_id": line_id , "request_id" : req_id},
                        dataType: 'json',
                    success: function(data){
                        console.log(data);
                        if(data.length != 0){
                          if(data[0].error == 1){
                              notifymsg("danger",data[0].msg);
                          }else{
                          notifymsg("success",". . .Request Content Deleted Successfully");
                          generate_reqContents(req_id);
                          }
                         }else{
                           $('#tbody_editCon').empty();
                         } 
                      }
                    });
                swal("Deleted!", "Request content has been deleted.", "success");
              }
            });
          });
    
    $.validator.setDefaults({ ignore: ":hidden:not(select)" });
    $("#ed_add_content").validate({
        rules: {
                ed_seq: {
                  required: true
                },
                ed_item: {
                  required: true
                },
                ed_qty: {
                  required: true,
                  number:true
                }
              }
            });

    //$.validator.setDefaults({ ignore: ":hidden:not(select)" });
    $("#ed_req_data").validate({
        rules :{
                  ed_reqDate:{
                      required: true
                  },
                  ed_reqBy:{
                      required: true
                  },
                  ed_reqStat:{
                      required: true
                  }
              }
            });

    $('#add_con_ed').click(function () {
      if($("#ed_add_content").valid() == true){
         $.ajax({
              url: "<?php echo base_url($this->session->userdata('forajax') . '/insert_req_content_edit'); ?>",
              method:'post',
              data: $('#ed_add_content').serialize(),
              dataType:'json',
              success: function (data) {
                  if(data[0].error == 1){
                      notifymsg("danger",data[0].msg);
                  }else{
                      generate_reqContents(req_id);
                      $("#item_fk option:first").attr('selected','selected').trigger('chosen:updated');
                      $('#ed_add_content')[0].reset();
                  }  
              }
           });
        }
    });

    $('#edit_req_btn').click(function (){
      if($('#ed_req_data').valid() == true){
        $.ajax({
          url:"<?php echo base_url($this->session->userdata('forajax') . '/update_request'); ?>",
          method: 'post',
          data: $('#ed_req_data').serialize(),
          dataType: 'json',
          success : function (data){
              if(data[0].error != 1){
                notifymsg("success",". . .Request Updated Successfully");
                close_edit(); active_search();
              }else{
                notifymsg("danger",data[0].msg);
              }
          }
        });
      }
    });


    $('#approve_req').click(function(){
      $.ajax({
            type: "POST",
            url: "<?php echo base_url($this->session->userdata('forajax').'/approve_request'); ?>",
            data:{'request_id':req_id , 'request_stat' : $('#aprv_s').val() },
            dataType: 'json',
        success: function(data){
                  if(data[0].error == 1){
                    notifymsg("danger",data[0].msg);
                  }else{
                      $('#approve_req').attr('disabled','disabled');
                      $('#cancel_apprv').html('Complete');
                  }
                }      
            });
    });



      function alert_cancel(){
            if($('#ed_req_data').serialize()!=$('#ed_req_data').data('serialize')){
                swal({
                    title: "Are you sure?",
                    text: "Your work will not be save",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Leave",
                    cancelButtonText: "Stay",
                    closeOnConfirm: true,
                    closeOnCancel: true },
                function (isConfirm) {
                    if (isConfirm) {
                        close_edit(); active_search();
                        notifymsg("hide","hide");
                    } else {
                        swal("Cancelled");
                    }
                });
                }else{
                close_edit(); active_search();
                notifymsg("hide","hide");
            }
        } 

        $('#cancel_apprv').click(function () {
            close_edit(); active_search();
        });

        $('#cancel_add').click(function () {
          if($('#cancel_add').html() == "Complete"){
            close_edit(); active_search();
            notifymsg("hide","hide");
          }else{            
            alert_cancel();
          }
        });


       $('#editX').click(function(){
           alert_cancel();
       });
    });
 </script>  